<template>
  <view class="orderDetail-container page_bg_FAFAFA">
    <view class="padd30">

      <view v-if="defaultAddress === null" class="marginT20 addressBtn">

        <button @click="chooseAddress" class="btn">+请选择地址</button>
      </view>
      <view v-else class="marginT20" @click="chooseAddress">
        <com-address :addressInfo="defaultAddress"></com-address>
      </view>

      <view class="marginT20">
        <com-goodsInfo :goodsInfo="goodsData" :price="goodsData.price"></com-goodsInfo>
      </view>



    </view>

    <view class="submit">
      <view class="score">
        工学分
        <view class="score-num">
          {{query.price}}
        </view>
      </view>
      <button @click="submit" class="btn">
        提交订单
      </button>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        query: {
          goodsId: '',
          price: '',
          addressId: '',
        },
        goodsData: {},
        defaultAddress: {},
        quantity: 1
      };
    },
    onLoad() {
      this.query.goodsId = uni.getStorageSync('goodsInfo').id
      this.query.price = uni.getStorageSync('goodsInfo').price


      this.getSimpleGoods()
      this.getDefaultAddress()
    },
    onShow() {
      this.getDefaultAddress();
    },
    methods: {
      async chooseAddress() {
        uni.navigateTo({
          url: '/subPages/selectAddress/selectAddress'
        })

      },
      async getSimpleGoods() {
        try {
          const res = await this.$http.get(`/yaolexue-service-portal/order/productSimpleInfo/${this.query.goodsId}`);
          if (res.code === 200) {
            this.goodsData = res.data;
          } else {
            uni.showToast({
              title: res.message,
              icon: 'none'
            })
          }
        } catch (error) {
          console.error('获取商品信息时发生错误:', error);
        }

      },

      async getDefaultAddress() {
        try {
          const res = await this.$http.get('/yaolexue-service-portal/receiveAddress/findMemberReceiveAddressDefault');

          if (res.code === 200) {
            this.defaultAddress = res.data;
            if (this.defaultAddress) {
                    this.query.addressId = this.defaultAddress.id;
                  }
          } else {
            uni.showToast({
              title: '地址选择失败',
              icon: 'error'
            });
          }

          // 如果没有默认地址，尝试从本地存储获取
          if (!this.defaultAddress) {
            const localAddress = uni.getStorageSync('addressInfo');
            if (localAddress) {
              this.defaultAddress = localAddress;
            }
          }

          console.log('res', res);
          console.log('this.query.addressId', this.query.addressId);

        } catch (error) {
          console.error('获取默认地址时发生错误:', error);
        }
      },

      submit() {
        if (this.defaultAddress === null) {
          uni.showToast({
            title: '请选择地址',
            icon: 'error'
          })
        } else {
          const queryParams =
            `goodsId=${this.query.goodsId}&price=${this.query.price}&addressId=${this.query.addressId}`;
          uni.navigateTo({
            url: `/subPages/payment/payment?${queryParams}`
          });
        }

      }
    }
  }
</script>

<style lang="scss">
  .orderDetail-container {

    .addressBtn {
      width: 689rpx;
      height: 152rpx;
      background: #FFFFFF;
      border-radius: 10rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      .btn {
        width: 561rpx;
        height: 86rpx;
        background: #5ac725;
        border-radius: 43rpx;
        color: #FFFFFF;
      }
    }

    .submit {
      width: 750rpx;
      height: 121rpx;
      background: #FFFFFF;
      display: flex;
      justify-content: space-between;
      align-items: center;
      position: fixed;
      bottom: 0;
      z-index: 999;

      .score {

        height: 30rpx;
        font-size: 26rpx;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #333333;
        display: flex;
        margin-left: 51rpx;

        .score-num {
          width: 40rpx;
          height: 19rpx;
          font-size: 26rpx;
          font-family: Source Han Sans CN;
          font-weight: 400;
          color: #FA312A;
          margin-left: 14rpx;
        }
      }

      .btn {
        width: 205rpx;
        height: 56rpx;
        line-height: 56rpx;
        background: #FA312A;
        border-radius: 28rpx;

        font-size: 30rpx;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #FFFFFF;
        margin-right: 90rpx;
      }
    }
  }
</style>